package com.vtb.thread;

import java.util.concurrent.BlockingQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.renren.api.client.param.impl.AccessToken;
import com.tencent.weibo.api.T_API;
import com.tencent.weibo.utils.WeiBoConst;
import com.vtb.base.VtbBaseReadThread;
import com.vtb.entity.DataCache;
import com.vtb.entity.QqOauth;
import com.vtb.entity.RrOauth;
import com.vtb.entity.SsOauth;
import com.vtb.servlet.WtbException;

/**
 * 写线程
 * @author Chen Tao
 * @date 2012-7-3 上午10:48:52
 */
public class VtbWriteThread extends VtbBaseReadThread{
    private static final Log log = LogFactory.getLog(VtbWriteThread.class);
    @SuppressWarnings("rawtypes")
    private Class clazz ;
    private T_API t_api = new T_API();  
    
    public VtbWriteThread(BlockingQueue<Object> send_queue){
        this.send_queue = send_queue;
    }
    public void run() {
        while (true) {
            try {
                clazz = send_queue.take().getClass();
                if (clazz.isInstance(RrOauth.class)) {
                    rrWrite((RrOauth)send_queue.take());
                }
                if (clazz.isInstance(QqOauth.class)) {
                    qqWrite((QqOauth)send_queue.take());
                }
                if (clazz.isInstance(SsOauth.class)) {
                    ssWrite((SsOauth)send_queue.take());
                }
                DataCache.mcountIncrement();
            } catch (Exception e) {
                log.error("发送消息失败：", e);
                throw new WtbException("发送消息异常", e);
            }
        }
    }
    
    /**
     * 人人状态发送方法
     *
     * @param rrOauth
     * @throws Exception
     * @author Chen Tao
     * @date 2012-7-3 上午11:48:37
     */
    private void rrWrite(RrOauth rrOauth) throws Exception{
        if (rrOauth.getImageUrl() == null || rrOauth.getImageUrl().equals("")) {
            client.getStatusService().setStatus(rrOauth.getMessage(),new AccessToken(rrOauth.getAccess_token()));
        }else {
            client.getPhotoService().uploadURLImg(0, rrOauth.getImageUrl(), rrOauth.getMessage(),new AccessToken(rrOauth.getAccess_token()));
        }
        DataCache.qqlastTime.replace(rrOauth.getRefq(), rrOauth.getLastTime());
    }
    
    /**
     * 腾讯微博发送方法
     *
     * @param qqOauth
     * @throws Exception
     * @author Chen Tao
     * @date 2012-7-3 上午11:48:57
     */
    private void qqWrite(QqOauth qqOauth) throws Exception{
        oauth.setOauth_token(qqOauth.getOauth_token());
        oauth.setOauth_token_secret(qqOauth.getOauth_token_secrete());
        if (qqOauth.getImageUrl()==null || "".equals(qqOauth.getImageUrl())) {
            t_api.add(oauth, WeiBoConst.ResultType.ResultType_Json, qqOauth.getMessage(), "");
        }else {
            t_api.add_pic(oauth, WeiBoConst.ResultType.ResultType_Json, qqOauth.getMessage(), "", "", "", qqOauth.getImageUrl());
        }
        DataCache.rrlastTime.replace(qqOauth.getRefr(), qqOauth.getLastTime());
    }
    
    /**
     * 新浪微博发送方法
     *
     * @param ssOauth
     * @throws Exception
     * @author Chen Tao
     * @date 2012-7-3 上午11:49:10
     */
    private void ssWrite(SsOauth ssOauth) throws Exception{
        
    }
}
